/*
 * 创建日期 2009-12-18
 *
 * 成都天和软件公司
 * 电话：028-85425861
 * 传真：028-85425861-8008
 * 邮编：610041 
 * 版权所有
 */
package  org.fantasy.common.grid.part;

import  org.fantasy.common.grid.bean.ReportParam;

import javax.servlet.http.HttpServletRequest;


/**
 *  菜单栏
 * @author: 王文成
 * @version: 1.0
 * @since 2009-12-18
 */
public class GridTablePart extends BaseReportPart{
    
    public GridTablePart( ReportParam param , HttpServletRequest request){
        super(param,request);
    } 
    
    @Override
    public StringBuffer getPager() {
        String ctx = request.getContextPath();
        String var = context.getVar();
        String pagerVar = context.getPagerVar();
        StringBuffer html = new StringBuffer(512);
        boolean isPage = context.isPage();
        html.append("<tbody " + (isPage?"":"style='display:none'")  + ">");
        html.append("<tr><td height=20>\n");
        html.append("       <script type=\"text/javascript\" src=\"" + ctx + "/js/pager.js\"></script> \n");
        html.append("       <script type=\"text/javascript\"> \n");
        html.append("       jQuery( function(){ \n");
        html.append("           var option = {}; \n");
        html.append("           option.isPage=" + isPage + "; \n");
        html.append("           option.pageSize=" + context.getPageSize() + "; \n");
        html.append("           option.pageStart=" + context.getPageStart() + "; \n");
        html.append("           option.pageCount=" + context.getPageCount() + "; \n");
        html.append("           option.dataSize=" + context.getDataSize() + "; \n");
        html.append("           option.pagerID='" + context.getPagerId() + "'; \n");
        html.append("           option.sql=jQuery('#" + context.getSqlId() + "').val();\n");
        html.append("           option.parameter=jQuery('#" + context.getRequestParamId() + "').val();\n");
        html.append("           option.action='" + context.getAction() + "'; \n");
        html.append("           option.countAction='" + context.getCountAction() + "'; \n");
        html.append("           window['" + pagerVar + "'] = ims.PagerUtil.init(option); \n");
        html.append("           window['" + pagerVar + "'].Export = function(){window['"+ var + "'].Export();};\n");
        html.append("       }); \n");
        html.append("       </script> \n");
        html.append("       <div id='" + context.getPagerId() + "'></div>\n ");
        html.append("</td></tr>\n");
        html.append("</tbody>\n");
        return html;
    }

    @Override
    public StringBuffer getScript() {
        String id = context.getId();
        String var = context.getVar();
        String ctx = request.getContextPath();
        String pagerVar = context.getPagerVar();
        StringBuffer html = new StringBuffer(512);
        html.append("<script type=\"text/javascript\" src='" + ctx + "/js/grid.js'></script>\n");
        html.append("<script type=\"text/javascript\"> \n");
        html.append("(function(){ \n");
        html.append("   var _PARAM = {}; \n");
        html.append("   _PARAM.tableID = '" + id + "'; \n");
        html.append("   _PARAM.tableConfig = '" + context.getTableConfig() + "'; \n");
        html.append("   _PARAM.isRowsSelect = true; \n");
        html.append("   _PARAM.isRowsHover = false; \n");
        html.append("   _PARAM.isHeadHover = true; \n");
        html.append("   _PARAM.isSortable = true; \n");
        html.append("   _PARAM.isHideCols = true; \n");
        html.append("   _PARAM.selectMode = '" + param.getSelectMode() + "'; \n");
        html.append("   window['" + var + "'] = GridUtil.createTable(_PARAM);\n");
        html.append("   window['" + var + "'].lockHead(" + param.isLockHead() + ");\n");
        html.append("   window['" + var + "'].Export = function(){  \n");
        html.append("       var pager = window['" + pagerVar + "']; \n");
        html.append("       if( pager.isComplete() ){\n");
        html.append("           if( pager.getCount() == 0 ) return; \n");
        html.append("           var OPTION = {};  \n");
        html.append("           OPTION.count = pager.getCount();  \n");
        html.append("           OPTION.win = window;  \n");
        html.append("           OPTION.sql = document.getElementById('" + context.getSqlId() + "').value;  \n");
        html.append("           OPTION.param = document.getElementById('" + context.getTagParamId() + "').value;  \n");
        html.append("           GridUtil.initExport(OPTION);  \n");
        html.append("       }else{ \n");
        html.append("           Message.alert('正在加载查询信息，请稍候.....'); \n");
        html.append("       } \n");
        html.append("   }  \n");
        html.append("})();\n");
        html.append("</script> \n");
        html.append("<script type=\"text/javascript\"> \r\n ");
        html.append("       jQuery( function(){ \n");
        html.append("            jQuery('#" + id + " .rdetail').click( function(i){ \n");
        html.append("                var id = jQuery(this).parent().parent().attr(\"id\"); \n");
        html.append("                var values = []; \n");
        html.append("                jQuery('#' + id + ' td').each( function(){ \n");
        html.append("                    if( jQuery('.rdetail',this).length == 0 ){ \n");
        html.append("                       var th = jQuery('#' + this.col); \n");
        html.append("                       if( th.attr('detail') != 'false'){ \n");
        html.append("                           var name = th.text(); \n");
        html.append("                           var value = jQuery(this).text(); \n");
        html.append("                           values.push({name:name,value:value}) \n");
        html.append("                       } \n");
        html.append("                    } \n");
        html.append("                }); \n");
        html.append("                window._RowView(values); \n");
        html.append("                jQuery(window).unload(function(){ top.Win.close(); } ); \n");
        html.append("            }); \n");
        html.append("       }); \n");
        html.append("</script>\r\n ");
        return html;
    }
}